﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using Clarius.Silverlight.Streaming.Library.Operations;

namespace Clarius.Silverlight.Streaming.Library
{
    public class FileSet
    {
        String name;
        bool isDeleted = false;

        List<File> fileList;
        AccountManager parent;
        bool isApplication;

        internal FileSet(String name, bool isApplication, AccountManager parent)
        {
            this.name = name;
            this.parent = parent;
            this.isApplication = isApplication;
        }

        internal bool IsDeleted
        {
            get
            {
                return isDeleted;
            }
        }

        internal AccountManager Parent
        {
            get
            {
                return parent;
            }
        }

        public File this[String filename]
        {
            get
            {
                ThrowIfDeleted();
                foreach (File file in Files)
                {
                    if (file.Name == filename)
                    {
                        return file;
                    }
                }

                return null;
            }
        }

        public int Count
        {
            get
            {
                ThrowIfDeleted();
                return Files.Count;
            }
        }

        public List<File> Files
        {
            get
            {
                ThrowIfDeleted();
                if (fileList == null)
                {
                    RetrieveFileListOperation operation = new RetrieveFileListOperation(this);
                    fileList = (List<File>) operation.Execute();
                }

                return fileList;
            }
        }

        public bool IsApplication
        {
            get { 
                ThrowIfDeleted();
                return isApplication;
            }
        }

        public String Name
        {
            get {
                ThrowIfDeleted();
                return name;
            }
        }

        public void DownloadFile(String fileName, String filePath)
        {
            ThrowIfDeleted();
        }

        public void Delete()
        {
            ThrowIfDeleted();
            DeleteFileSetOperation operation = new DeleteFileSetOperation(this.parent, name);
            operation.Execute();
            isDeleted = true;
        }

        /// <summary>
        /// Returns the size of the File Set calculated by adding the size of each of the contained files
        /// </summary>
        public long Size
        {
            get
            {
                long totalSize = 0;

                foreach (File f in Files)
                {
                    totalSize += f.Size;
                }

                return totalSize;
            }
        }

        void ThrowIfDeleted()
        {
            if (isDeleted)
            {
                throw new SilverlightStreamingException(String.Format("FileSet '{0}' was previously deleted",name), null);
            }
        }
    }
}
